package leecode

import "sort"

func maximumHappinessSum(happiness []int, k int) int64 {
	sort.Ints(happiness)
	var result int
	index := 1
	for i := 0; i < k; i++ {
		result += happiness[len(happiness)-1]
		happiness = happiness[:len(happiness)-1]
		if len(happiness) == 0 {
			break
		}
		happiness[len(happiness)-1] = happiness[len(happiness)-1] - index
		if happiness[len(happiness)-1] <= 0 {
			happiness[len(happiness)-1] = 0
		}

		index++
	}
	return int64(result)
}
